% write_basic_term_occ.pro

% intended for inclusion in module write_basic_term

:- style_check(-atom).          % allow long atoms


%------------------------------------------------------------------------------
% WRITE BASIC TERM AS LaTeX occurrence labelled FORMAT
%------------------------------------------------------------------------------

%   write_basic_term_occ(+Term)
%
%   write latex occ text describing term
%   Term is a basic term represented as a typed prolog term
%
write_basic_term_occ(Term) :-
    basic_term_occ(Term,'').



basic_term_occ(X::T, Occ) :-
    X =.. [abstraction|Ps],
    !,
    format('~w:',Occ),
    write_basic_term(untyped, X::T),
    format('\n'),
    %
    atom_concat(Occ,1,Occ1),
    atom_concat(Occ,2,Occ2),
    forall(
        member(P-M,Ps),
        (
         basic_term_occ(P,Occ1),
         basic_term_occ(M,Occ2)
        )
    ).

basic_term_occ(X::T, Occ) :-
    X =.. [tuple|Ps],
    !,
    format('~w:',Occ),
    write_basic_term(untyped, X::T),
    format('\n'),
    %
    length(Ps,L),
    numlist(1,L,Nums),
    forall(
        member(Num,Nums),
        (
         nth1(Num,Ps,P),
         atom_concat(Occ,Num,OccNum),
         basic_term_occ(P,OccNum)
        )
    ).

basic_term_occ(X::T, Occ) :-
    X =.. [_C|Ps],
    !,
    % constructor
    format('~w:',Occ),
    write_basic_term(untyped, X::T),
    format('\n'),
    %
    (Ps == []
    ->  true
    ;
        length(Ps,L),
        numlist(1,L,Nums),
        forall(
            member(Num,Nums),
            (
             nth1(Num,Ps,P),
             atom_concat(Occ,Num,OccNum),
             basic_term_occ(P,OccNum)
            )
        )
    ).


